<template>
  <el-radio-group
    v-model="radioValue"
    v-bind="$attrs"
    :size="size"
    @change="handleChange"
  >
    <component
      :is="radioComponent"
      v-for="option in normalizedOptions"
      :key="option.value"
      :label="option.value"
      :disabled="option.disabled"
      :border="border"
    >
      {{ option.label }}
    </component>
  </el-radio-group>
</template>

<script setup>
import { computed } from 'vue'
import { ElRadioGroup, ElRadio, ElRadioButton } from 'element-plus'

const props = defineProps({
  modelValue: [String, Number, Boolean],
  options: {
    type: Array,
    default: () => []
  },
  size: String,
  border: Boolean,
  button: Boolean // 是否使用按钮样式
})

const emit = defineEmits(['update:modelValue', 'change'])

const radioValue = computed({
  get() {
    return props.modelValue
  },
  set(value) {
    emit('update:modelValue', value)
  }
})

// 标准化选项格式
const normalizedOptions = computed(() => {
  return props.options.map(option => {
    if (typeof option === 'string' || typeof option === 'number' || typeof option === 'boolean') {
      return {
        label: String(option),
        value: option,
        disabled: false
      }
    }
    return {
      label: option.label || String(option.value),
      value: option.value,
      disabled: option.disabled || false
    }
  })
})

const radioComponent = computed(() => {
  return props.button ? ElRadioButton : ElRadio
})

const handleChange = (value) => {
  emit('change', value)
}
</script>